package com.core.app.window.view.data;

import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.vaadin.dialogs.ConfirmDialog;

import com.core.app.event.CallbackEvent;
import com.core.app.window.view.detail.DetailKelulusanSekolahView;
import com.core.app.window.view.detail.DetailSekolahView;
import com.core.context.Inject;
import com.core.entity.Kelulusan;
import com.core.entity.Sekolah;
import com.core.service.KelulusanService;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.util.BeanContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.Runo;

public class DataKelulusanView extends CustomComponent {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private Table kelulusanTable;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_2;
	@AutoGenerated
	private Button deleteKelulusanButton;
	@AutoGenerated
	private Button editKelulusanButton;
	@AutoGenerated
	private Button insertKelulusanButton;
	@Autowired
	private KelulusanService kelulusanService;
	private static final long serialVersionUID = 5334051609927820674L;
	private Map<String, Kelulusan> kelulusanSekolah;
	private Sekolah sekolah;

	/**
	 * The constructor should first build the main layout, set the composition
	 * root and then do any custom initialization.
	 * 
	 * The constructor will not be automatically regenerated by the visual
	 * editor.
	 */
	public DataKelulusanView() {
		Inject.Inject(this);
		buildMainLayout();
		setCompositionRoot(mainLayout);

		// TODO add user code here
		// INSERT
		insertKelulusanButton.addListener(new ClickListener() {
			private static final long serialVersionUID = -3607457987136357009L;

			@Override
			public void buttonClick(ClickEvent event) {
				final Window w = DataKelulusanView.this.getApplication()
						.getMainWindow();
				Window insert = new Window("Insert Data Kelulusan");
				insert.setModal(true);
				insert.setResizable(false);
				insert.setContent(new DetailKelulusanSekolahView(
						new CallbackEvent() {

							@Override
							public void callback(Object object) {
								Kelulusan kel = (Kelulusan) object;
								kel.setSekolah(sekolah);
								if (kelulusanSekolah.containsKey(kel
										.getTahunAjaran()) == false) {
									kelulusanSekolah.put(kel.getTahunAjaran(),
											kel);
								}
								initTable();
							}
						}));
				w.addWindow(insert);
			}
		});

		// EDIT
		editKelulusanButton.addListener(new ClickListener() {

			private static final long serialVersionUID = 2927520415703576308L;

			@Override
			public void buttonClick(ClickEvent event) {
				final Window w = DataKelulusanView.this.getApplication()
						.getMainWindow();
				Window insert = new Window("Edit Data Kelulusan");
				insert.setModal(true);
				insert.setResizable(false);
				DetailKelulusanSekolahView detailKelulusanView = new DetailKelulusanSekolahView(
						new CallbackEvent() {

							@Override
							public void callback(Object object) {
								Kelulusan kel = (Kelulusan) object;
								kelulusanSekolah.put(kel.getTahunAjaran(), kel);
								initTable();
							}
						});
				detailKelulusanView.setKelulusan(kelulusanSekolah
						.get(kelulusanTable.getValue()));
				insert.setContent(detailKelulusanView);
				w.addWindow(insert);
			}
		});

		// DELETE
		deleteKelulusanButton.addListener(new ClickListener() {

			private static final long serialVersionUID = -6613787183549572965L;

			@Override
			public void buttonClick(ClickEvent event) {
				ConfirmDialog.show(DataKelulusanView.this.getApplication().getMainWindow(), "Are you sure to delete?",
						new ConfirmDialog.Listener() {

							@Override
							public void onClose(ConfirmDialog dialog) {
								if (dialog.isConfirmed()) {

									Kelulusan kelulusan = kelulusanSekolah
											.get(kelulusanTable.getValue());
									if (kelulusan.getId() != null) {
										try {
											kelulusanService.delete(kelulusan);
										} catch (Exception e) {
											getWindow().showNotification(
													e.getMessage());
											e.printStackTrace();
										}
									}
									kelulusanSekolah.remove(kelulusanTable
											.getValue());
									initTable();
								}

							}
						});
			}
		});
	}

	private void initTable() {
		BeanContainer<Long, Kelulusan> kelulusanContainer = new BeanContainer<Long, Kelulusan>(
				Kelulusan.class);

		kelulusanContainer.setBeanIdProperty("tahunAjaran");
		kelulusanTable.setContainerDataSource(kelulusanContainer);
		kelulusanTable.setColumnHeader("tahunAjaran", "Tahun Ajaran");
		kelulusanTable.setColumnHeader("jumlahLakiLaki", "Siswa (Orang)");
		kelulusanTable.setColumnHeader("jumlahPerempuan", "Siswi (Orang)");
		kelulusanTable.setColumnHeader("jumlah", "Jumlah");
		kelulusanTable.setVisibleColumns(new String[] { "tahunAjaran",
				"jumlahLakiLaki", "jumlahPerempuan", "jumlah" });
		kelulusanTable.setSelectable(true);
		kelulusanTable.addStyleName(Runo.TABLE_SMALL);

		if (kelulusanSekolah != null) {

			kelulusanContainer.addAll(kelulusanSekolah.values());
		}
	}

	public Map<String, Kelulusan> getKelulusanSekolah() {
		Set<String> ids = (Set<String>) kelulusanSekolah.keySet();
		for (String string : ids) {
			Kelulusan k = kelulusanSekolah.get(string);
			System.out.println(k.getTahunAjaran() + ":" + k.getJumlahLakiLaki()
					+ ":" + k.getJumlahPerempuan());
		}
		return kelulusanSekolah;
	}

	public void setKelulusanSekolah(Map<String, Kelulusan> kelulusanSekolah) {
		this.kelulusanSekolah = kelulusanSekolah;
		initTable();
	}

	public Sekolah getSekolah() {
		return sekolah;
	}

	public void setSekolah(Sekolah sekolah) {
		this.sekolah = sekolah;
	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("-1px");
		mainLayout.setHeight("-1px");
		mainLayout.setMargin(true);
		mainLayout.setSpacing(true);

		// top-level component properties
		setWidth("-1px");
		setHeight("-1px");

		// horizontalLayout_2
		horizontalLayout_2 = buildHorizontalLayout_2();
		mainLayout.addComponent(horizontalLayout_2);

		// kelulusanTable
		kelulusanTable = new Table();
		kelulusanTable.setImmediate(false);
		kelulusanTable.setWidth("-1px");
		kelulusanTable.setHeight("290px");
		mainLayout.addComponent(kelulusanTable);

		return mainLayout;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_2() {
		// common part: create layout
		horizontalLayout_2 = new HorizontalLayout();
		horizontalLayout_2.setImmediate(false);
		horizontalLayout_2.setWidth("-1px");
		horizontalLayout_2.setHeight("-1px");
		horizontalLayout_2.setMargin(false);
		horizontalLayout_2.setSpacing(true);

		// insertKelulusanButton
		insertKelulusanButton = new Button();
		insertKelulusanButton.setCaption("Add");
		insertKelulusanButton.setImmediate(true);
		insertKelulusanButton.setWidth("75px");
		insertKelulusanButton.setHeight("-1px");
		horizontalLayout_2.addComponent(insertKelulusanButton);

		// editKelulusanButton
		editKelulusanButton = new Button();
		editKelulusanButton.setCaption("Edit");
		editKelulusanButton.setImmediate(true);
		editKelulusanButton.setWidth("75px");
		editKelulusanButton.setHeight("-1px");
		horizontalLayout_2.addComponent(editKelulusanButton);

		// deleteKelulusanButton
		deleteKelulusanButton = new Button();
		deleteKelulusanButton.setCaption("Delete");
		deleteKelulusanButton.setImmediate(true);
		deleteKelulusanButton.setWidth("75px");
		deleteKelulusanButton.setHeight("-1px");
		horizontalLayout_2.addComponent(deleteKelulusanButton);

		return horizontalLayout_2;
	}

}
